<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Lixunguan\Yuntongxun\Sdk as Yuntongxun;

/**
 * 容联云的短信发送
 * @data $phone $type[1:注册状态码 2:登录状态码 3:修改密码状态码] [$tmp]
 * @return status status message captcha validperiod
 */
class SendMessage extends Controller
{
    public function sendPhoneMessage($phone, $type, $tmp = '1')
    {
        // 准备你的 appid sid authtoken
        $appId =  env('APP_ID');
        $sid = env('ACCOUNT_SID');
        $authtoken =  env('AUTH_TOKEN');

        // 登陆容联云
        $sdk = new Yuntongxun($appId, $sid, $authtoken);

        // 验证码长度
        $codelength = '6';

        // 准备验证码随机数
        $rand = rand(pow(10,($codelength-1)), pow(10,$codelength)-1);

        // 发送信息
        $sms = $sdk->sendTemplateSMS($phone, array($rand), $tmp);

        $time = time() + 90;

        // 判断状态
        if ($sms->statusCode == '000000') {
            // 成功返回
            return $status = [
                'status'      => '200',
                'type'        => $type,
                'msg'         => '验证码发送成功',
                'captcha'     => $rand,
                'validperiod' => $time,
            ];
        }

        // 失败返回
        return $status = [
            'status'      => '500',
            'type'        => $type,
            'msg'         => '验证码发送失败',
            'captcha'     => null,
        ];
    }
}
